|
|
|
|
|
|
|
Figure 3.3
Program CopyDIB uses an array of RGBTriplet values to manipulate a DIB. |
|
|
|
|
|
|
|
|
If memory on your system is scarce, you may need to use color palettes. The rest of this chapter explains color palettes and how you can manage them. The rest of the example programs described in this chapter were designed to work with 256 color palettes. If your computer uses some other color model, the programs may not work normally. Some present an error message and refuse to run. |
|
|
|
|
|
|
|
|
Ideally, your program could specify a pixel and a color, and your computer would make the pixel that color. This is the way it works in color models with more than 8 bits per pixel. |
|
|
|
|
|
|
|
|
Some, but not all, systems that use fewer bits per pixel can still produce a wide range of colors, but they can display only a limited number of colors at one time. EGA systems use two bits to specify each of the red, green, and blue components of a color in hardware. Because they use a total of six bits to store the colors, EGA systems can distinguish only 26 = 64 different colors. In other words, an EGA system can produce only 64 distinct colors. |
|
|
|
|
|
|
|
|
VGA and Super-VGA displays use six bits to store each of the red, green, and blue components of a color. Because they use a total of 18 bits to define colors to the hardware, these displays can distinguish up to 218 = 262,144 different colors. |
|
|
|
|
|
|
|
|
Even with all of these colors, an 8-bit per pixel VGA system can display only 256 different colors at one time. To keep track of the colors that are in use at any given moment, the system uses a system color palette. This palette lists all the colors that the system is prepared to display. For a VGA system, the palette contains 256 entries, each containing an 18-bit color value. To each pixel on the screen, the system assigns an index into the system palette. To find out the color of a pixel, you can look up the pixel's value in the palette. Figure 3.4 shows how the color palette maps pixels to colors. |
|
|
|
|
|
|
|
|
When the system is told to make a pixel a particular color, the system assigns the pixel the index of the color in the palette. If the requested color is not in the palette and the palette is not full, the system adds it. |
|
|
|
|
|
|
|
|
Things become complicated if the color is not in the palette and the palette is full. In that case, the system must either replace an entry in the palette or use the closest available |
|
|
|
|
|